package com.hisoft.entity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.hisoft.type.ResultEnum;
import com.hisoft.utility.DataProviderTool;
import com.thoughtworks.selenium.SeleniumException;

public class Function extends ABaseTag {

	Logger logger = Logger.getLogger(Function.class);
	//command标签的值
	private String command;
	//target标签的值
	private String target;
	//value标签的值
	private String value;

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	/**
	 * 执行
	 */
	/**
	 * 执行该对象前，把临时变量替换掉
	 * 
	 * <b></b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-8-30
	 */
	private void replaceProperty() {// 执行该对象前，把临时变量替换掉
		this.target = DataProviderTool.getProperty(target);
		this.value = DataProviderTool.getProperty(value);
	}

	@Override
	public String excute() {
		beforeExcute();
		replaceProperty();// 导入临时变量
		String returnStr = "" + ResultEnum.FAIL;
		logger.info(toString());
		try {
			try {
				Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] { String.class });
				returnStr = (String) f.invoke(xmlDriveSelenium, target);
			} catch (NoSuchMethodException e) {
				try {
					Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] { String.class, String.class });
					returnStr = (String) f.invoke(xmlDriveSelenium, target, value);
				} catch (NoSuchMethodException e1) {
					Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] {});
					returnStr = (String) f.invoke(xmlDriveSelenium);
				}
			}
			append(returnStr);
			this.setResult(ResultEnum.PASS);
		} catch (SecurityException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalArgumentException e) {
			logger.error(e.getMessage(), e);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage(), e);
		} catch (InvocationTargetException e) {
			Throwable target = e.getTargetException();
			if (target instanceof SeleniumException) {
				returnStr = "LINE: " + this.getLineNumb() + ", " + ((SeleniumException) target).getMessage();
				logger.error(returnStr);
				append(returnStr);
				this.setResult(ResultEnum.FAIL);
				// throw (SeleniumException) target;
			} else {
				logger.error(e.getMessage(), e);
			}
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage(), e);
		}
		return returnStr;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return command + " | " + target + " | " + value;
	}

	public Function clone() {
		Function h = new Function();
		h.setCommand(this.command);
		h.setLineNumb(this.getLineNumb());
		h.setTarget(this.target);
		h.setValue(this.value);
		h.setInfo(this.getInfo());
		return h;
	}
}
